<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-06 00:34:26
 * @LastEditTime: 2019-09-06 10:48:45
 * @LastEditors: Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/vant.css">
    <link rel="stylesheet" type="text/css" href="../css/base.css" />
    <script type="text/javascript" src="../script/jquery.js"></script>
    <script type="text/javascript" src="../script/layer/mobile/layer.js"></script>
    <script type="text/javascript" src="../script/vue.min.js"></script>
    <script type="text/javascript" src="../script/vant.min.js"></script>
    <script type="text/javascript" src="../script/fastclick.js"></script>
    <script type="text/javascript" src="../script/base.js"></script>
    <style>
        .update {
            background: #fff;
        }

        .update .title {
            font-weight: 700;
        }

        .progress {
            width: 85%;
            padding: 30px 0;
            background: transparent;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <div class="init-full">
            <van-nav-bar class="head" title="设置" left-arrow @click-left="onClickLeft" />
            </van-nav-bar>
            <div class="body-no-bottom">
                <div class="content">
                    <van-cell value="软件版本" @click="showVersion" style="margin-top: 10px;"></van-cell>
                    <van-cell value="检测更新" @click="checkUpdate" style="margin-top: 10px;"></van-cell>
                </div>
                <van-loading v-if="showLoading" />
            </div>

            <van-popup style="width: 85%;border-radius: 4px;" v-model="showUpdateInfo" :overlay="true"
                :lazy-render="false">
                <div class="update">
                    <van-cell-group>
                        <van-cell class="title" value="软件更新"></van-cell>
                        <van-field v-model="updateInfo.version" label="版本号" type="text" readonly></van-field>
                        <van-field v-model="updateInfo.updateTip" label="更新内容" type="textarea" autosize readonly>
                        </van-field>
                        <van-field v-model="updateInfo.time" label="发布时间" type="text" readonly></van-field>
                    </van-cell-group>
                    <div>
                        <van-button type="primary" block @click="toUpdate">确定</van-button>
                    </div>
                </div>
            </van-popup>

            <van-popup class="progress" v-model="showProgress" :overlay="true" :lazy-render="false" :close-on-click-overlay="false">
                <van-progress :percentage="progressValue" :show-pivot="false" />
            </van-popup>

        </div>
    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {
        //$('.init-full').show();
        vm = new Vue({
            el: '#app',
            data: {
                showLoading: false,
                showUpdateInfo: false,
                updateInfo: [],
                showProgress: false,
                progressValue: 0
            },
            created: function () {
                var that = this;
            },
            methods: {
                showVersion() {
                    var that = this;
                    that.$toast('当前版本：' + api.appVersion);
                },
                checkUpdate() {
                    var that = this;
                    that.showLoading = true;
                    that.progressValue = 0;
                    var mam = api.require('mam');
                    mam.checkUpdate(function (ret, err) {
                        that.showLoading = false;
                        if (ret) {
                            var result = ret.result;
                            if (result.update == true && result.closed == false) {
                                if (api.systemType == "android") {
                                    that.updateInfo = result;
                                    that.showUpdateInfo = true;

                                } else {
                                    api.installApp({
                                        appUri: result.source
                                    });
                                }

                            } else {
                                that.$toast('暂无更新!');
                            }
                        } else {
                            that.$toast('暂无更新!');
                        }
                    });
                },
                toUpdate() {
                    var that = this;
                    that.showUpdateInfo = false;
                    that.showProgress = true;
                    api.download({
                        url: that.updateInfo.source,
                        report: true
                    }, function (ret, err) {
                        if (ret && 0 == ret.state) {/* 下载进度 */
                            that.progressValue = parseInt(ret.percent);
                        }
                        if (ret && 1 == ret.state) {/* 下载完成 */
                            that.showProgress = false;
                            that.progressValue = 100;
                            var savePath = ret.savePath;
                            setTimeout(function () {
                                api.installApp({
                                    appUri: savePath
                                });
                            }, 500)


                        }
                    });
                },
                onClickLeft() {
                    api.closeWin();
                }
            }
        })
    };
</script>

</html>